import java.util.*;

public class Test {

    //前k个单词
    //1.统计每个单词出现的次数
    public List<String> topKFrequent(String[] words,int k) {
        Map<String, Integer> map = new HashMap<>();
        for (String word : words) {
            if (map.get(word) == null) {
                map.put(word, 1);
            } else {
                int val = map.get(word);
                map.put(word, val + 1);
            }
        }
        //2. 建立小根堆
        PriorityQueue<Map.Entry<String,Integer>> minHeap = new PriorityQueue<>
                (new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().compareTo(o2.getValue()) == 0){
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue()-o2.getValue();
            }
        });
        //3. 遍历map 调整优先级队列
        for(Map.Entry<String,Integer> entry:map.entrySet()){
            if(minHeap.size() < k){
                minHeap.offer(entry);
            }else{
                Map.Entry<String,Integer> top = minHeap.peek();
                //如果当前频率相同
                if(top.getValue().compareTo(entry.getValue()) == 0){
                    //字母顺序小的进来
                    if(top.getKey().compareTo(entry.getKey()) > 0 ){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }else{
                    if(top.getValue().compareTo(entry.getValue()) < 0){
                        minHeap.poll();
                        minHeap.offer(entry);
                    }
                }
            }
        }
        List<String> ret = new ArrayList<>();
        for(int i = 0; i < k; i++){
            Map.Entry<String,Integer> top = minHeap.poll();
            ret.add(top.getKey());
        }
        Collections.reverse(ret);
        return ret;

    }
    //2.
    /*
    * 统计单词出现的次数
    * this man this woman happy day happy
    * */
    public static void main(String[] args) {
        String[] word = {"this","man","mars","concert","MARS","CONCERT","fire","mars"
        };
        func(word);
    }
    public static void func(String[] words) {
        Map<String,Integer> map = new HashMap<>();
        for (String word: words) {
            if(map.get(word) == null){
                map.put(word,1);
            }else{
                int val = map.get(word);
                map.put(word,val+1);
            }
        }
        System.out.println(map);
    }
    public static void main7(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()){
            String s1 = in.nextLine();
            String s2 = in.nextLine();
            func(s1,s2);
        }
    }
    public static void func(String str1, String strAct){
        Set<Character> set1 = new HashSet<>();
        for (char ch:strAct.toUpperCase().toCharArray()) {
            set1.add(ch);
        }
        Set<Character> set2 = new HashSet<>();
        for (char ch : str1.toUpperCase().toCharArray()) {
            if(!set1.contains(ch) && !set2.contains(ch)){
                System.out.print(ch);
                set2.add(ch);
            }
        }
    }



    public static void main6(String[] args) {
        Person person1 = new Person("08042");
        Person person2 = new Person("08042");
        HashBuck2<Person,String> hashBuck2 = new HashBuck2<>();
        hashBuck2.put(person1,"zhansgan");
        System.out.println(hashBuck2.get(person1));//zhansgan id值一样Person1和person是一个人 输出名字相等
        System.out.println(hashBuck2.get(person2));//zhansgan
       /* System.out.println(person1.hashCode());//46045125
        System.out.println(person2.hashCode());//46045125*/
    }
    public static void main5(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1,1);
        hashBuck.put(3,3);
        hashBuck.put(13,13);
        hashBuck.put(5,5);
        hashBuck.put(6,6);
        hashBuck.put(16,6);
        hashBuck.put(8,6);
        hashBuck.put(18,6);

        System.out.println("======");

    }
    public static void main4(String[] args) {
        Set<String> set = new TreeSet<>();
        set.add("hello");
        set.add("MARS");
        set.add("CONCERT");
        System.out.println(set);
        for(String s : set){
            System.out.println(s);
        }
    }
    public static void main3(String[] args) {
        Map<String, Integer> map = new TreeMap<>();
        TreeMap<String, Integer> map2 = new TreeMap<>();

        map2.put("this", 3);

        map2.put("phe", 5);

        map2.put("a", 7);
        map2.put("a", 8888);
        //有的话输出val 没有的输出1999
        int val = map2.getOrDefault("phe8", 1999);

        System.out.println(val);


        Set<String> set = map2.keySet();


        Collection<Integer> collection = map2.values();

        System.out.println("===");

        Set<Map.Entry<String, Integer>> entries = map2.entrySet();

         for (Map.Entry<String, Integer> entry : entries) {
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
    }
    public static void main1(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        binarySearchTree.insert(3);
        binarySearchTree.insert(6);
        binarySearchTree.insert(8);
        binarySearchTree.insert(5);

        binarySearchTree.remove(6);
        System.out.println("==");

    }
}
